package net.versiforma.libraryreminder.librarycontrollers;

import java.util.ArrayList;

import net.versiforma.libraryreminder.BorrowedItem;
import net.versiforma.libraryreminder.LibraryAccount;


import com.thoughtworks.selenium.DefaultSelenium;


public class VoebbController implements LibraryInterface {

	public DefaultSelenium selenium;
	private LibraryAccount account;
	private static final String HOMEPAGE_URL = "https://www.voebb.de";
		
	public VoebbController(LibraryAccount account) {
		this.account = account;
		selenium = new DefaultSelenium("localhost", 4567, "*firefox", HOMEPAGE_URL);
	}


	public void start() {
		selenium.start();
		// Open the website
		selenium.open("");
//		selenium.setSpeed("1000");
	}


	public boolean maintenancePageShown() {
		if (selenium.isElementPresent("//h1[contains(@text, 'Wartung')]")) return true;
		else return false;
	}

	
	String usernameFieldLocator = "L#AUSW_1";
	
	public void login() {
		// Go to my account page
		selenium.click("//a[contains(.,'Mein Konto')]");
		selenium.waitForPageToLoad("30000");
		// Login
		 
		selenium.type(usernameFieldLocator, account.getUserid());
		selenium.type("LPASSW_1", account.getPassword());
		selenium.click("textButton");
		selenium.waitForPageToLoad("30000");
		
		// Click "Ok" is any message is shown
		if (selenium.isElementPresent("//div[@class='message']")) {
			selenium.click("//input[@name='messageButton']");
			selenium.waitForPageToLoad("30000");
		}
	}
	
	
	public boolean loginFailedPageShown() {
		if (
				// Login fields are still visible
				selenium.isElementPresent(usernameFieldLocator)
		) return true;
		else return false;
	}
	
	
	public boolean noBorrowedItems() {
		if (selenium.isTextPresent("Keine Ausleihen"))
			return true;
		return false;
	}
	
	
	public ArrayList<BorrowedItem> getBorrowedItems() {

		String DATE_FORMAT = "dd.MM.yyyy";
		int items_counter;
		ArrayList<BorrowedItem> borrowedItems = new ArrayList<BorrowedItem>();

		// Get today's date
		//			String todayStr = selenium.getText("//div[@id='OPACLI']/div/div/table/tbody/tr[2]/td").trim();
		//			todayStr = todayStr.split("\\s+")[1];

		// Open borrowings
		selenium.click("//a[contains(text(),' zeigen oder verlängern')]");
		selenium.waitForPageToLoad("30000");

		// Count borrowed items on shown page
		items_counter = selenium.getXpathCount("//table[@class=\"rTable_table\"]/tbody/tr").intValue();

		// Collect data about the borrowed items
		for (int i=1; i<=items_counter; i++) {
			BorrowedItem item = new BorrowedItem();
			borrowedItems.add(item);
			String dueDateString = selenium.getText("//table[@class=\"rTable_table\"]/tbody/tr["+i+"]/td[2]");
			item.setDueDate((dueDateString.contains("-")? dueDateString.split("-")[1].trim() : dueDateString.trim()), DATE_FORMAT);
			item.setLibrary(selenium.getText("//table[@class=\"rTable_table\"]/tbody/tr["+i+"]/td[3]").trim());
			item.setTitle(selenium.getText("//table[@class=\"rTable_table\"]/tbody/tr["+i+"]/td[4]").trim());
		}
		
		return borrowedItems;

	}
	
	public void logout() {
		// Go back to my account page 
		selenium.click("//input[@value='Benutzerkonto schließen']");
		selenium.waitForPageToLoad("30000");
	}
	
	public void close() {
		selenium.stop();
	}

	
}
